﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace OSSSearchRefiners.CONTROLTEMPLATES.OSSSearchRefiners {
    public partial class OSSSearchResultsRefiners : UserControl {
      
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {

                //This page load method will fire on each page IF the page uses a masterpage with the AdditionalPageHead
                //delegate control. Only continue if the request is for the OOTB osssearchresults.aspx page
                string localPath = HttpContext.Current.Request.Url.LocalPath;
                
                if (localPath.Equals("/_layouts/OSSSearchResults.aspx", StringComparison.CurrentCultureIgnoreCase)) {
                    //Create a new requestURL using the current web url and the current path and query.
                    string requestUrl = SPContext.Current.Web.Url + HttpContext.Current.Request.Url.PathAndQuery;

                    //replace the OOTB osssearchresults.aspx layouts location with the custom osssearchresults 
                    //page in this solution
                    string redirectUrl = requestUrl.Replace(localPath, "/_layouts/cusrefiners/OSSSearchResults.aspx");

                    //redirect the response to our custom osssearchresults page
                    Response.Redirect(redirectUrl);
                }
            }
        }
    }
}
